package cn.com.open.ms.ecgpaasdatarest.dynamicsql;

import cn.com.open.ms.ecgpaasdatarest.dynamicsql.impl.*;
import cn.com.open.ms.ecgpaasdatarest.enums.SQLEnums;

/**
 * @Description 动态SQL工厂
 * @author 奶茶
 * Created by on 2018/11/29 11:36.
 */
public class DynamicSqlFactory {

    public static StringBuilder joinSQL(String parameterName,String sqlValue,StringBuilder selectConditionBuilder) {
        Object[] split = sqlValue.split("\\.",sqlValue.indexOf("."));
        ParseParameter parseParameter = getParseParameterInstance(split[0].toString());
        String sql = parseParameter.analysisParameterBySQLEnums(parameterName, split[1]);
        return selectConditionBuilder.append(sql);
    }

    /**
     * 根据sqlType选择加载实现类
     * @param sqlType
     * @return
     */
    private static ParseParameter getParseParameterInstance(String sqlType){

        if(sqlType.equals(SQLEnums.EQ.getSqlAlias())){
            return new EqualSqlManagement();
        }else if (sqlType.equals(SQLEnums.GTE.getSqlAlias())){
            return new GTEqualSqlManagement();
        }else if (sqlType.equals(SQLEnums.GT.getSqlAlias())){
            return new GTSqlManagement();
        }else if(sqlType.equals(SQLEnums.LT.getSqlAlias())){
            return new LTSqlManagement();
        }else if(sqlType.equals(SQLEnums.LTE.getSqlAlias())){
            return new LTEqualSqlManagement();
        }else if(sqlType.equals(SQLEnums.NEQ.getSqlAlias())){
            return new NEqualSqlManagement();
        }else{
            return new INSqlManagement();
        }
    }

}
